package com.example.farmeri.ui.base;

import android.content.Context;
import android.content.DialogInterface;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.farmeri.R;
import com.example.farmeri.presenter.base.BasePresenter;

import java.lang.reflect.Method;

/**
 * Create by weiqishan on 2020/3/18.
 */
public class BaseActivity<K extends BasePresenter> extends AppCompatActivity{

    protected final String TAG = this.getClass().getSimpleName();

    protected Toolbar mToolbar;

    public Toolbar getToolbar() {
        return mToolbar;
    }

    protected K present;
//    protected V view;

    /**
     * 使用ToolBar
     * @param v toolBar的地址
     * @param mTitle    主标题
     * @param isCenter  主标题是否居中
     */
    @Deprecated
    protected void initToolBar(int v, String mTitle, Boolean isCenter, Boolean hasNav) {
        mToolbar = findViewById(v);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mToolbar.setTitle(mTitle);
        mToolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.colorWhite));
        //返回导航
        if (hasNav){
            mToolbar.setNavigationContentDescription("返回");
            mToolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_24dp);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
        //主标题居中
        if (false){
        //if (isCenter){
            //设置标题居中
            TextView title = (TextView) mToolbar.getChildAt(0);
            title.getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;
            title.setGravity(Gravity.CENTER_HORIZONTAL);
        }

    }


    /**
     * 使用ToolBar
     * @param v toolBar的地址
     * @param mTitle    主标题
     */
    @Deprecated
    protected void initToolBar(int v, String mTitle, Boolean hasNav) {
        mToolbar = findViewById(v);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mToolbar.setTitle(mTitle);
        mToolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.colorWhite));
        //返回导航
        if (hasNav){
            mToolbar.setNavigationContentDescription("返回");
            mToolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_24dp);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }

    }


    /**
     * 让文字与图标一同显示
     * @param featureId
     * @param menu
     * @return
     */
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return super.onMenuOpened(featureId, menu);
    }


    protected AlertDialog initBaseDialog(Context context, String msg){
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setTitle("提示");
        dialog.setMessage(msg);
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        return dialog;
    }

    protected void showToast(Context context, String s){
        Toast.makeText(context,s,Toast.LENGTH_SHORT).show();
    }
    protected void showToast(String s){
        Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
    }


}
